package com.sundablog.controller.backend.system.upms.permissions;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sundablog.pojo.AdminPermission;
import com.sundablog.pojo.AdminRoleExample;
import com.sundablog.pojo.AdminUser;
import com.sundablog.result.BaseResult;
import com.sundablog.result.LayUiResult;
import com.sundablog.result.ZTreeResult;
import com.sundablog.service.backend.system.upms.permissions.PermissionsService;
import com.sundablog.utlis.RedisUtil;

/**
 * 权限管理控制器
 * @ClassName:  PermissionsController   
 * @Description:权限管理控制器   
 * @author: 哒哒 
 * @date:   2018年3月14日 下午7:10:13   
 *     
 * @Copyright: 2018 www.sundablog.com Inc. All rights reserved.
 */
@Controller
@RequestMapping("/backend/system/upms/permissions")
public class PermissionsController {

	@Autowired
	private PermissionsService permissionsService;
	
	/**
	 * 权限界面显示
	 * @Title: index   
	 * @Description: 界面显示 
	 * @param: @return      
	 * @return: String      
	 * @throws
	 */
	@RequiresPermissions("backend_system_upms_permissions_index")
	@RequestMapping("/index")
	public String index() {	
		return "backend/system/upms/permissions/index";
	}
	
	/**
	 * 添加根界面显示
	 * @Title: add   
	 * @Description: 添加界面显示   
	 * @param: @return      
	 * @return: String      
	 * @throws
	 */
	@RequiresPermissions("backend_system_upms_permissions_add")
	@RequestMapping("/add")
	public String add() {
		//清空Redis
		AdminUser user = (AdminUser) SecurityUtils.getSubject().getPrincipal();
		RedisUtil.remove("backend_menu_"+user.getUserId());
		RedisUtil.remove("backend_reaim_"+user.getUserId());
		
		return "backend/system/upms/permissions/add";
	}
	
	/**
	 * 添加子菜单界面显示
	 * @Title: addPermissions   
	 * @Description: 添加子菜单界面显示  
	 * @param: @return      
	 * @return: String      
	 * @throws
	 */
	@RequiresPermissions("backend_system_upms_permissions_addPermissions")
	@RequestMapping("/addPermissionsIndex")
	public String addPermissionsIndex() {
		//清空Redis
		AdminUser user = (AdminUser) SecurityUtils.getSubject().getPrincipal();
		RedisUtil.remove("backend_menu_"+user.getUserId());
		RedisUtil.remove("backend_reaim_"+user.getUserId());
		
		return "backend/system/upms/permissions/addPermissions";
	}
	
	/**
	 * 编辑界面显示
	 * @Title: edit   
	 * @Description: TODO(这里用一句话描述这个方法的作用)   
	 * @param: @return      
	 * @return: String      
	 * @throws
	 */
	@RequiresPermissions("backend_system_upms_permissions_edit")
	@RequestMapping("/edit")
	public String edit() {
		//清空Redis
		AdminUser user = (AdminUser) SecurityUtils.getSubject().getPrincipal();
		RedisUtil.remove("backend_menu_"+user.getUserId());
		RedisUtil.remove("backend_reaim_"+user.getUserId());
		
		return "backend/system/upms/permissions/edit";
	}
	
	/**
	 * 删除一条权限
	 * @Title: deletePermission   
	 * @Description: 删除一条权限   
	 * @param: @param permissionId
	 * @param: @return      
	 * @return: BaseResult      
	 * @throws
	 */
	@RequiresPermissions("backend_system_upms_permissions_deletePermission")
	@RequestMapping("/deletePermission")
	@ResponseBody
	public BaseResult deletePermission(int permissionId) {
		//清空Redis
		AdminUser user = (AdminUser) SecurityUtils.getSubject().getPrincipal();
		RedisUtil.remove("backend_menu_"+user.getUserId());
		RedisUtil.remove("backend_reaim_"+user.getUserId());
		
		int i = permissionsService.deletePermission(permissionId);
		if (i > 0) {
			return BaseResult.build(200, BaseResult.delectSuccess);
		} else {
			return BaseResult.build(209, BaseResult.delectFail);
		}
	}
	
	/**
	 * 禁用一条权限(注意:如果当前状态是禁用,如调用此方法会变成启用)
	 * @Title: updataDisablePermissionId   
	 * @Description: 禁用一条权限(注意:如果当前状态是禁用,如调用此方法会变成启用)
	 * @param: @param permissionId
	 * @param: @return      
	 * @return: BaseResult      
	 * @throws
	 */
	@RequiresPermissions("backend_system_upms_permissions_updataDisablePermissionId")
	@RequestMapping("/updataDisablePermissionId")
	@ResponseBody
	public BaseResult updataDisablePermissionId(int permissionId) {
		//清空Redis
		AdminUser user = (AdminUser) SecurityUtils.getSubject().getPrincipal();
		RedisUtil.remove("backend_menu_"+user.getUserId());
		RedisUtil.remove("backend_reaim_"+user.getUserId());
		
		int i = permissionsService.updataDisablePermissionId(permissionId);
		if (i > 0) {
			return BaseResult.build(200, BaseResult.updataSuccess);
		} else {
			return BaseResult.build(209, BaseResult.updataFail);
		}
	}

	
	
	
	/**
	 * 图标显示
	 * @Title: larryfont   
	 * @Description: 图标显示   
	 * @param: @return      
	 * @return: String      
	 * @throws
	 */
	@RequestMapping("/larryfont")
	public String larryfont() {
		return "backend/system/upms/permissions/larryfont";
	}
	
	
	/**
	 * 查询权限
	 * @Title: findPagingQuery   
	 * @Description: 查询权限   
	 * @param: @return      
	 * @return: LayUiResult      
	 * @throws
	 */
	@RequestMapping("/findPagingQuery")
	@ResponseBody
	public LayUiResult findPagingQuery() {
		LayUiResult result = permissionsService.findPagingQuery();
		
		return result;
	}
	
	/**
	 * 添加权限
	 * @Title: addPermissions   
	 * @Description: 添加权限   
	 * @param: @param adminPermission
	 * @param: @return      
	 * @return: BaseResult      
	 * @throws
	 */
	@RequestMapping("/addPermissions")
	@ResponseBody
	public BaseResult addPermissions(AdminPermission adminPermission) {
		int i = permissionsService.addPermissions(adminPermission);
		if (i > 0) {
			return BaseResult.build(200, BaseResult.addSuccess);
		} else {
			return BaseResult.build(209, BaseResult.addFail);
		}
	}
	

	
	/**
	 * 根据id查询一条权限数据
	 * @Title: selectPermissionsByPermissionId   
	 * @Description: 根据id查询一条权限数据 
	 * @param: @param permissionId
	 * @param: @return      
	 * @return: BaseResult      
	 * @throws
	 */
	@RequestMapping("/selectPermissionsByPermissionId")
	@ResponseBody
	public BaseResult selectPermissionsByPermissionId(int permissionId) {
		AdminPermission adminPermission = permissionsService.selectPermissionsByPermissionId(permissionId);
		return BaseResult.build(200, "", adminPermission);
	}
	
	/**
	 * 修改权限
	 * @Title: updataPermissions   
	 * @Description: 修改权限  
	 * @param: @param permissionId
	 * @param: @return      
	 * @return: BaseResult      
	 * @throws
	 */
	@RequestMapping("/updataPermissions")
	@ResponseBody
	public BaseResult updataPermissions(AdminPermission adminPermission) {
		int i = permissionsService.updataPermissions(adminPermission);
		if (i > 0) {
			return BaseResult.build(200, BaseResult.updataSuccess);
		} else {
			return BaseResult.build(200, BaseResult.updataFail);
		}
	}
	
	/**
	 * 查询全部权限返回ztree需要的结构
	 * @Title: selectPermissions   
	 * @Description: 查询全部权限返回ztree需要的结构
	 * @param: @return      
	 * @return: List<ZTreeResult>      
	 * @throws
	 */
	@RequestMapping("/selectPermissions")
	@ResponseBody
	public BaseResult selectPermissions(){
		List<ZTreeResult> list = permissionsService.selectPermissions();
		return BaseResult.build(200, "", list);
	}
}
